﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ServiceModel.Syndication;
using System.Xml;

namespace BlogKickStart.Controllers
{
    public class RssFeedController : Controller
    {
        //
        // GET: /RssFeed/

        public ActionResult Index()
        {
            SyndicationFeed feed =
            new SyndicationFeed("CS 4790",
                        "This is a test feed",
                        new Uri("http://stefanholmberg.com"),
                        "TestFeed1",
                        DateTime.Now);

            SyndicationItem item =
                new SyndicationItem("Feed Title",
                                    "This is the content for Test Feed",
                                    new Uri("http://stefanholmberg.com/post/2012/08/13/The-new-blog.aspx"),
                                    "TestItem1",
                                    DateTime.Now);

            List<SyndicationItem> items = new List<SyndicationItem>();
            items.Add(item);
            feed.Items = items;

            return new RssActionResult() { Feed = feed };
        }

    }

    public class RssActionResult : ActionResult
    {
        public SyndicationFeed Feed { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ContentType = "application/rss+xml";

            Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);
            using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
            {
                rssFormatter.WriteTo(writer);
            }
        }
    }


}
